﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SQModule
{
    private List<TeamRoleInfo> _data = new List<TeamRoleInfo>();
    public int lastOutType;//当前打出的牌型
    public int teamState;
    public int passNum = 0;
    public CardVo showCard;
    public int showCardRoleId1;
    public int showCardRoleId2;
    public int showCardIndex1;
    public int showCardIndex2;
    public ChangePosVo changePosVo;
    public int nowOutCardPos;
    public int lastOutCardPos;
    public List<CardVo> lastOutCards = new List<CardVo>();
    public ShuangQResultVo resultVo;
    private List<CardVo> _cardList = new List<CardVo>();
    public void UpdateTeamRoleInfo(ArrayList al)
    {
        int count = (int)al[0];
        _data.Clear();
        if (count == 0)
            return;
        ArrayList als = (ArrayList)al[1];
        int i = 0;
        for (i = 0; i < count; i++)
        {
            TeamRoleInfo vo = new TeamRoleInfo();
            vo.Update((ArrayList)als[i]);
            _data.Add(vo);
        }
    }

    public void UpdateTeamState(ArrayList al)
    {
        teamState = (int)al[0];
    }

    public void UpdateTeamOffLine(ArrayList al)
    {
        int roleId = (int)al[0];
        TeamRoleInfo vo = GetTeamRoleInfoById(roleId);
        if (vo != null)
            vo.offLine = true;
    }

    public void UpdateTeamRoleReady(ArrayList al)
    {
        int count = (int)al[0];
        if (count == 0)
            return;
        ArrayList als = (ArrayList)al[1];
        int i = 0;
        int roleId;
        bool ready;
        for (i = 0; i < count; i++)
        {
            ArrayList als1 = (ArrayList)als[i];
            roleId = (int)als1[0];
            ready = (bool)als1[1];
            TeamRoleInfo vo = GetTeamRoleInfoById(roleId);
            if (vo != null)
                vo.ready = ready;
        }
    }

    public void UpdateTeamRoleAuto(ArrayList al)
    {
        int count = (int)al[0];
        if (count == 0)
            return;
        ArrayList als = (ArrayList)al[1];
        int i = 0;
        int roleId;
        bool auto;
        for (i = 0; i < count; i++)
        {
            ArrayList als1 = (ArrayList)als[i];
            roleId = (int)als1[0];
            auto = (bool)als1[1];
            TeamRoleInfo vo = GetTeamRoleInfoById(roleId);
            if (vo != null)
                vo.auto = auto;
        }
    }

    public void UpdateShowCard(ArrayList al)
    {
        showCard = new CardVo();
        showCard.yello = (short)al[0];
        showCard.num = (short)al[1];
        showCardRoleId1 = (int)al[2];
        showCardRoleId2 = (int)al[3];
        showCardIndex1 = (int)al[4];
        showCardIndex2 = (int)al[5];
        nowOutCardPos = GetTeamRoleInfoById(showCardRoleId1).pos;
    }

    public void UpdateChangePos(ArrayList al)
    {
        changePosVo = new ChangePosVo();
        changePosVo.Update(al);
        TeamRoleInfo vo = GetTeamRoleInfoById(changePosVo.roleId1);
        vo.pos = changePosVo.pos1;
        TeamRoleInfo vo1 = GetTeamRoleInfoById(changePosVo.roleId2);
        vo1.pos = changePosVo.pos2;
    }

    public int mePos
    {
        get
        {
            #region
            return 1;
            #endregion
            // return GetTeamRoleInfoById(DataPool.instance.roleVo.id).pos;
        }
    }

    public void UpdateNowOutCardpos(ArrayList al)
    {
        nowOutCardPos = (int)al[0];
        Debug.Log("nowOutCardPos: " + nowOutCardPos);
        lastOutCardPos = (int)al[1];
        lastOutCards.Clear();
        lastOutType = (int)al[2];
        int count = (int)al[3];
        if (count == 0)
        {
            passNum++;
            return;
        }
        passNum = 0;
        ArrayList als = (ArrayList)al[4];
        int i = 0;
        bool meOut = (lastOutCardPos == mePos);
        CardVo card;
        for (i = 0; i < count; i++)
        {
            ArrayList als1 = (ArrayList)als[i];
            card = new CardVo();
            card.yello = (short)als1[0];
            card.num = (short)als1[1];
            lastOutCards.Add(card);
            if (meOut)
            {
                CardVo meCard = _cardList.Find(p => p.num == card.num && p.yello == card.yello);
                _cardList.Remove(meCard);
            }
        }
        TeamRoleInfo vo = GetTeamRoleInfoByPos(lastOutCardPos);
        vo.cardNum -= count;
    }

    public void UpdateDealCard(ArrayList al)
    {
        int count = (int)al[0];
        _cardList.Clear();
        if (count == 0)
            return;
        ArrayList als = (ArrayList)al[1];
        int i = 0;
        CardVo card;
        for (i = 0; i < count; i++)
        {
            ArrayList als1 = (ArrayList)als[i];
            card = new CardVo();
            card.yello = (short)als1[0];
            card.num = (short)als1[1];
            _cardList.Add(card);
        }
    }

    public void UpdateShuangQResult(ArrayList al)
    {
        resultVo = new ShuangQResultVo();
        resultVo.shuangqType = (int)al[0];
        int count = (int)al[1];
        if (count == 0)
            return;
        ArrayList als = (ArrayList)al[2];
        int i = 0;
        ShuangQResultBaseVo baseVo;
        for (i = 0; i < count; i++)
        {
            ArrayList als1 = (ArrayList)als[i];
            baseVo = new ShuangQResultBaseVo();
            baseVo.roleId = (int)als1[0];
            baseVo.outTime = (int)als1[1];
            baseVo.gold = (int)als1[2];
            resultVo.list.Add(baseVo);
        }
    }

    public class ShuangQResultVo
    {
        public int shuangqType;
        public List<ShuangQResultBaseVo> list = new List<ShuangQResultBaseVo>();
    }

    public class ShuangQResultBaseVo
    {
        public int roleId;
        public int outTime;
        public int gold;
    }

    public class CardVo
    {
        public short yello;
        public short num;
    }

    public class TeamRoleInfo
    {
        public int pos;
        public int roleId;
        public int templateId;
        public string roleName;
        public int level;
        public int gold;
        public bool ready = false;
        public bool auto = false;
        public bool offLine = false;
        public int cardNum = 27;
        public void Update(ArrayList data)
        {
            pos = (int)data[0];
            roleId = (int)data[1];
            templateId = (int)data[2];
            roleName = (string)data[3];
            level = (int)data[4];
            gold = (int)data[5];
        }
    }
    public List<CardVo> GetAllCard()
    {
        return _cardList;
    }

    public List<TeamRoleInfo> GetAllTeamRoleInfo()
    {
        return _data;
    }

    public TeamRoleInfo GetTeamRoleInfoByPos(int pos)
    {
        TeamRoleInfo vo = _data.Find(p => p.pos == pos);
        return vo;
    }

    public TeamRoleInfo GetTeamRoleInfoById(int id)
    {
        TeamRoleInfo vo = _data.Find(p => p.roleId == id);
        return vo;
    }

    public class ChangePosVo
    {
        public int roleId1;
        public int pos1;
        public int roleId2;
        public int pos2;
        public void Update(ArrayList data)
        {
            roleId1 = (int)data[0];
            pos1 = (int)data[1];
            roleId2 = (int)data[2];
            pos2 = (int)data[3];
        }
    }
}
